//
//  AmusementViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "AmusementViewController.h"//美图

#import "ScienceTechnologyModel.h"//数据
#import "AmusementSubViewController.h"//点击内容调转子界面阅读



@interface AmusementViewController ()<UIPickerViewDataSource,UIPickerViewDelegate, iCarouselDataSource,iCarouselDelegate,UIActionSheetDelegate>

@property (nonatomic, strong) iCarousel *carousel;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *pickerData;
@property (nonatomic, strong) UILabel *pickerLabel;

@property (nonatomic, strong) GroundGlass *backImage;
@property (nonatomic, strong) NSMutableArray *imageArray;//预装图片
@property (nonatomic, copy) NSString *nextUrl;//点击更多
@property (nonatomic) NSInteger topCount;//点击更多的次数
@end

@implementation AmusementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"美图鉴赏";
    //设置毛玻璃效果
    self.backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleLight];
    [self.view addSubview:self.backImage];
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kP6Height(485),kWidth, kP6Height(50))];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.center = CGPointMake(kP6width(375)/2,kP6Height(570));
    self.pickerView.showsSelectionIndicator = YES;

    [self.view addSubview:self.pickerView];
    self.pickerData = @[@"魅惑丽人",@"小小萝莉",@"性感模特",@"速度激情",@"花瓣美女",@"激情四色",@"唯美天使",@"麻豆姑娘",@"向往着太阳"];
    
    self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, kP6Height(90),kWidth,kP6Height(460))];
    self.carousel.type = iCarouselTypeCoverFlow2;
    [self.view addSubview:self.carousel];
    self.carousel.dataSource = self;
    self.carousel.delegate = self;
    self.carousel.type = iCarouselTypeCylinder;//动画模式
    
    
    UIBarButtonItem *rightBarBut = [[UIBarButtonItem alloc] initWithTitle:@"点我更多" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButAction:)];
    self.navigationItem.rightBarButtonItem = rightBarBut;
}

#pragma mark iCarousel 方法
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    if (self.imageArray.count > 20)
    {
        AmusementModel *model = self.imageArray[2];
        self.nextUrl = model.next_url;
    }
    return self.imageArray.count;//返回的个数
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    AmusementModel *model = self.imageArray[index];
    AmusementModel *oneModel = self.imageArray[0];
    view = [[UIImageView alloc] initWithFrame:CGRectMake(0,kP6Height(0), kP6width(250), kP6Height(180))];
    view.contentMode = UIViewContentModeScaleAspectFill;
    if (model.thumbnail_mpic.length == 0) {
        NSArray *nextUrlArr = model.media;
        AmusementModel *nexurlModel = nextUrlArr[0];
        [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:nexurlModel.url]];
    }
    else{
        [self.backImage sd_setImageWithURL:[NSURL URLWithString:oneModel.thumbnail_mpic]];
        [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_mpic] placeholderImage:[UIImage imageNamed:@"5.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
    }
    return view;
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    if (option == iCarouselOptionSpacing)
    {
        return value * 1.1;
    }
    return value;
}

//点击图片时触发的方法
- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    AmusementModel *model = self.imageArray[index];
    AmusementSubViewController *amusementSubVC = [[AmusementSubViewController alloc] init];
    amusementSubVC.imageArr = model.media;
    [self.navigationController pushViewController:amusementSubVC animated:YES];
}

#pragma mark - pickerView
//当前显示的位置触发的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *rowLabel = (UILabel *)[pickerView viewForRow:row forComponent:0];
    rowLabel.font = [UIFont systemFontOfSize:kP6Height(20)];
    switch (row) {
        case 0:
            rowLabel.textColor =[UIColor purpleColor];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kBeautifulPictureURl collectionView:self.carousel];
            break;
        case 1:
            rowLabel.textColor = [UIColor cyanColor];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kSisterPictureURl collectionView:self.carousel];
            break;
        case 2:
            rowLabel.textColor = [UIColor magentaColor];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kModelPictureURL collectionView:self.carousel];
            break;
        case 3:
            rowLabel.textColor = [UIColor colorWithRed:0.447 green:0.000 blue:0.000 alpha:1.000];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kCarModelPictureURL collectionView:self.carousel];
            break;
        case 4:
            rowLabel.textColor = [UIColor magentaColor];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kPetalsBeautyURL collectionView:self.carousel];
            break;
        case 5:
            rowLabel.textColor = [UIColor redColor];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kBeautifulModelURL collectionView:self.carousel];
            break;
        case 6:
            rowLabel.textColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kAestheticismURL collectionView:self.carousel];
            break;
        case 7:
            rowLabel.textColor = [UIColor colorWithRed:0.502 green:0.000 blue:1.000 alpha:1.000];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kHempBeanURL collectionView:self.carousel];
            break;
        case 8:
            rowLabel.textColor = [UIColor orangeColor];
            self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kSunshineURL collectionView:self.carousel];
            break;
        default:
            break;
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;//设置分区
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.pickerData.count;//返回 行数
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    self.pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, kP6Height(20))];
    self.pickerLabel.textAlignment = NSTextAlignmentCenter;
    self.pickerLabel.textColor = [UIColor whiteColor];
    self.pickerLabel.font = [UIFont systemFontOfSize:kP6Height(18)];
    self.pickerLabel.text = self.pickerData[row];
    if (row == 0)
    {
    self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:kBeautifulPictureURl collectionView:self.carousel];
        UILabel *rowLabel = (UILabel *)[pickerView viewForRow:row forComponent:0];
        rowLabel.font = [UIFont systemFontOfSize:kP6Height(20)];
         rowLabel.textColor =[UIColor purpleColor];
    }
    self.topCount = 0;
    return self.pickerLabel;
}

#pragma mark - 当滑动完之后再次解析新的数椐
- (void)rightBarButAction:(UIBarButtonItem *)button
{

    NSInteger count = 0 ;
    if ([self.nextUrl isEqualToString:@"http://iphone.myzaker.com/zaker/blog2news.php?app_id=969&since_date=1424190845&nt=6&_appid=iphone"]||[self.nextUrl isEqualToString:@"http://iphone.myzaker.com/zaker/blog2news.php?app_id=969&since_date=1424190845&nt=6&_appid=iphone"]||[self.nextUrl isEqualToString:@"http://iphone.myzaker.com/zaker/blog2news.php?app_id=1156&since_date=1435742245&nt=11&_appid=iphone"]||[self.nextUrl isEqualToString:@"http://iphone.myzaker.com/zaker/blog2news.php?app_id=11026&since_date=1415244411&nt=3&_appid=iphone"]||[self.nextUrl isEqualToString:@"http://iphone.myzaker.com/zaker/blog2news.php?app_id=488&since_date=1425002892&nt=3&_appid=iphone"]) {
        count =1;
    }
    if (self.nextUrl.length==0 || count == 1)
    {
        self.topCount+=1;
        if (self.topCount <3)
        {
            [MBProgressHUD showError:@"已经到最后咯"];
        }
        else if (self.topCount==3)
        {
            [MBProgressHUD showError:@"不都告诉你了,没啦,还想看嘛!"];
        }
        else if (self.topCount==4)
        {
            [MBProgressHUD showError:@"真的没了"];
        }
        else if (self.topCount>4)
        {
            [MBProgressHUD showError:@"换一个看嘛"];
        }
        
    }
    else
    {
    self.imageArray = [ACHCommon beautifulPictureModelAsynchronousurl:self.nextUrl collectionView:self.carousel];
    }
 
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
